package com.ironman.h6b1gw.jxls;

import com.ironman.h6b1gw.jxls.model.Department;
import org.jxls.common.Context;
import org.jxls.util.JxlsHelper;

import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

public class Jxls1Demo {


    public static void main(String[] args) throws Exception {
        Path templatePath = Paths.get("C:/Users/89754/Desktop/jxls/template1.xlsx");
        Path outPath = Paths.get("C:/Users/89754/Desktop/jxls/out1.xlsx");
        if (Files.exists(outPath)) {
            Files.delete(outPath);
        }
        if (Files.exists(templatePath)) {
            List<Department> departments = Department.departments();
            try (InputStream templateStream = Files.newInputStream(templatePath)) {
                try (OutputStream outStream = Files.newOutputStream(outPath)) {
                    Context context = new Context();
                    context.putVar("departments", departments);
                    context.putVar("sheetNames", departments.stream().map(Department::getName).collect(Collectors.toList()));
                    JxlsHelper.getInstance().processTemplate(templateStream, outStream, context);
                }
            }
        } else {
            System.err.println("模板文件不存在");
        }
    }
}